package com.dr.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.dr.common.ValueUtil;
import com.dr.pojo.ConstantItem;
import com.dr.pojo.Dept;
import com.dr.pojo.Invoice;
import com.dr.pojo.Regist;
import com.dr.pojo.RegistLevel;
import com.dr.pojo.Scheduling;
import com.dr.pojo.User;
import com.dr.service.ConstantService;
import com.dr.service.DeptService;
import com.dr.service.InvoiceService;
import com.dr.service.RegistLevelService;
import com.dr.service.RegistService;
import com.dr.service.SchedulingService;
import com.dr.service.UserService;

@Controller
@RequestMapping("/rg")
public class RegistCtrl {
	@Autowired
	private UserService userService;//spring注入ServiceImp的实例
	
	@Autowired
	private DeptService deptService;//spring注入ServiceImp的实例
	
	@Autowired
	private ConstantService constantItemService;//spring注入ServiceImp的实例

	@Autowired
	private RegistLevelService registLevelService;
	
	@Autowired
	private InvoiceService invoiceService;
	
	@Autowired
	private RegistService registService;
	
	@Autowired
	private SchedulingService schedulingService;
	
	@RequestMapping("/regist")
	public String regist(Model md,Regist regist) {
		//发票号
		Invoice invoice = invoiceService.nextInvoice();
		if(invoice != null) {
			md.addAttribute("invoice", invoice);
		}else {
			md.addAttribute("invoice", new Invoice());
			md.addAttribute("errInvoice", "发票已用完，请重新申请");
		}
		//病历号
		String caseNumber = registService.nextCaseNumber();
		if(regist.getCaseNumber() == null) {//未查到数据
			md.addAttribute("caseNumber", caseNumber);//新病历号
		}else {
			md.addAttribute("caseNumber", regist.getCaseNumber()); //旧病历号
		}
		
		//挂号级别
		List<RegistLevel> registLevelList = registLevelService.findRegistLevel();
		
		if(registLevelList.size() != 0) {
			md.addAttribute("registLevelList", registLevelList);
		}else {
			md.addAttribute("registLevelList", new ArrayList<>());
			md.addAttribute("errRegistLevel", "获取挂号级别信息失败");
		}
		
		return "regist/regist.html";	
	}
	
	@RequestMapping("/caseInfo")
	public String caseInfo(Model md,String caseNumber) {
		if(ValueUtil.isEmpty(caseNumber)) {
			caseNumber="";
		}
		Regist regist = registService.findRegistByCaseNumber(caseNumber);
		
		if(regist == null) {
			regist = new Regist();
		}else {
			regist.setRegistID(0);//挂号级别
			regist.setDeptName("");//部门
		}
		md.addAttribute("regist", regist);
		return regist(md, regist);
	}
	
	@RequestMapping("/schedulingInfo")
	public String schedulingInfo(Model md,Regist regist) {
		//System.out.println("---------------------------");
		List<Scheduling> schedulingList = schedulingService.findSchedulingList(regist);
		//System.out.println(schedulingList);
		md.addAttribute("schedulingList", schedulingList);
		return regist(md, regist == null ? new Regist():regist);
	}
	
	@RequestMapping("/doRegist")
	public String doRegist(Model md, Regist regist, HttpSession session) {
		User loginUser = (User) session.getAttribute("loginUser");
		System.out.println(loginUser);
		System.out.println(loginUser.getId());
		regist.setRegistID(loginUser.getId());
		int count = registService.insertRegist(regist);
		if (count == 0) {
			md.addAttribute("errMsg", "挂号失败，请联系系统管理员");
		}
		else {
			invoiceService.updateInvoice(regist.getId(), regist.getInvoiceNum());
		}
		return "regist/success.html";
	}
	
	

}